import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/base/base_nav_bar.dart';
import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/common/not_found.dart';
import 'package:cbec_app/components/order/order_goods_item.dart';
import 'package:cbec_app/components/popup/baojia_success_popup.dart';
import 'package:cbec_app/pages/my/controller/after_sales_orderlist_ctr.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class Baojia extends GetView<AfterSalesOrderListController> {
  // final AfterSalesOrderListController afterSalesOrderListCtr =
  //     Get.put(AfterSalesOrderListController());

  Baojia({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AfterSalesOrderListController>(builder: (controller) {
      return BasePage(
        title: "保价",
        iconType: IconType.back,
        children: [
          Container(
            color: Tools.fromHex("#F86369"),
            height: 100.h,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                BaojiaTopItem(num: "${controller.total['count'] ?? 0}次", title: "累计成功", iconUrl: "yellow_coin.png"),
                SizedBox(width: 37.w),
                BaojiaTopItem(num: "${controller.total['successPrice'] ?? 0}元", title: "累计金额", iconUrl: "yellow_yes.png"),
                SizedBox(width: 37.w),
                BaojiaTopItem(num: "${controller.total['maxPrice'] ?? 0}元", title: "最高金额", iconUrl: "yellow_good.png"),
              ],
            ),
          ),
          _Tabs(),
          Container(
            height: 550.w,
            child: TabBarView(controller: controller.baojiaTabController, children: [
              Container(
                  child: Column(
                children: [
                  orderSearchBox(),
                  Container(
                      height: 480.w,
                      color: Tools.fromHex("#F3F5F7"),
                      // padding:EdgeInsets.only( bottom: 20.w),
                      child: controller.baojiaList.isNotEmpty
                          ? ListView.builder(
                              itemCount: controller.baojiaList.length,
                              itemBuilder: (context, index) {
                                final Map item = controller.baojiaList[index];
                                return baojiaGoodsItem(item: item);
                              })
                          : const NotFound()
                      // ListView(
                      //   shrinkWrap: true,
                      //   children: [
                      //     baojiaGoodsItem(),
                      //     baojiaGoodsItem()
                      //   ],
                      // ),
                      )
                ],
              )),
              Container(
                height: 550.w,
                child: Column(
                  children: [
                    Container(
                      margin: EdgeInsets.only(top: 10.w),
                      padding: EdgeInsets.only(left: 18.w, right: 18.w),
                      child: Row(
                        children: [
                          BaseInteractive(
                            onClick: () => {},
                            childWidget: Container(
                              decoration: border_radis(Tools.fromHex("#FBF1F0"), borderColor: Tools.fromHex("#F83F2D"), radiusValue: 20.w),
                              padding: EdgeInsets.fromLTRB(10.w, 5.w, 10.w, 5.w),
                              child: Row(
                                children: [Text("成功", style: setTextStyle(12, FontWeight.w400, Tools.fromHex("#F83F2D"))), Icon(Icons.expand_more, size: 15, color: Tools.fromHex("#F83F2D"))],
                              ),
                            ),
                          ),
                          SizedBox(width: 20.w),
                          BaseInteractive(
                            onClick: () => {},
                            childWidget: Container(
                              decoration: border_radis(Tools.fromHex("#FBF1F0"), borderColor: Tools.fromHex("#F83F2D"), radiusValue: 20.w),
                              padding: EdgeInsets.fromLTRB(10.w, 5.w, 10.w, 5.w),
                              child: Row(
                                children: [Text("近三个月", style: setTextStyle(12, FontWeight.w400, Tools.fromHex("#F83F2D"))), Icon(Icons.expand_more, size: 15, color: Tools.fromHex("#F83F2D"))],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    orderSearchBox(),
                    Container(
                      color: Tools.fromHex("#F3F5F7"),
                      // padding: EdgeInsets.only(
                      //     left: 8.w, right: 8.w, bottom: 20.w),
                      height: 370.h,
                      child: ListView(
                        children: [
                          // baojiaGoodsItem(type: 2)
                        ],
                      ),
                    )
                  ],
                ),
              ),
            ]),
          ),
        ],
      );
    });
  }

  /**
   *  选项卡
   */
  Widget _Tabs() => Container(
        margin: EdgeInsets.only(top: 10.w),
        height: 30,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            TabBar(
                dividerColor: Colors.transparent,
                indicator: const BoxDecoration(
                  border: Border(
                    bottom: BorderSide(width: 2, color: Colors.red),
                  ),
                ),
                labelColor: Tools.fromHex("#F83F2D"),
                isScrollable: true,
                unselectedLabelColor: Colors.black,
                controller: controller.baojiaTabController,
                tabAlignment: TabAlignment.start,
                tabs: const [
                  Tab(
                    text: "保价申请",
                  ),
                  Tab(
                    text: "申请记录",
                  )
                ]),
            Row(
              children: [Text("特殊场景不支持，请查看", style: setTextStyle(10, FontWeight.w300, Tools.fromHex("#666666"))), Text("保价规则", style: setTextStyle(10, FontWeight.w300, Tools.fromHex("#0256FF"))), SizedBox(width: 18.w)],
            )
          ],
        ),
      );

  //订单搜索框
  Widget orderSearchBox() {
    return Container(
      // height: 30.h,
      decoration: border_radis(Colors.white, radiusValue: 20.w),
      margin: EdgeInsets.only(left: 8.w, right: 8.w, top: 20.w, bottom: 10.w),
      padding: EdgeInsets.fromLTRB(10.w, 7.w, 10.w, 7.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              BaseImage(url: "vector.png", width: 16, height: 16),
              SizedBox(width: 5.w),
              Container(
                width: 200.w,
                // height: 30.h,
                child: TextFormField(
                  decoration: descript("搜索我的订单", fontsize: 16),
                ),
              )
            ],
          ),
          BaseInteractive(
            onClick: () => {Tools.showToast("清空输入框")},
            childWidget: BaseImage(url: "gray_close.png", width: 16, height: 16),
          )
        ],
      ),
    );
  }
}

class BaojiaTopItem extends StatelessWidget {
  final String num;

  final String title;

  final String iconUrl;

  const BaojiaTopItem({super.key, required this.num, required this.title, required this.iconUrl});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: Colors.white.withOpacity(0.3),
      ),
      width: 88.w,
      height: 88.w,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(num, style: setTextStyle(18, FontWeight.w300, Tools.fromHex("#FFFFFF"))),
          SizedBox(height: 14.h),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [BaseImage(url: iconUrl, width: 16, height: 16), SizedBox(width: 1.w), Text(title, style: setTextStyle(12, FontWeight.w300, Tools.fromHex("#FFFFFF")))],
          )
        ],
      ),
    );
  }
}

class baojiaGoodsItem extends StatelessWidget {
  final int? type;

  final Map item;

  final AfterSalesOrderListController controller = Get.find<AfterSalesOrderListController>();

  baojiaGoodsItem({super.key, this.type = 1, required this.item});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: border_radis(Colors.white),
      margin: EdgeInsets.fromLTRB(8.w, 10.w, 8.w, 10.w),
      padding: EdgeInsets.all(8.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [Text("订单编号：${item['sn'] ?? ''}", style: setTextStyle(13, FontWeight.w300, Tools.fromHex("#333333"))), Text("下单时间:${item['cd'] ?? ''}", style: setTextStyle(10, FontWeight.w300, Tools.fromHex("#333333")))],
          ),
          SizedBox(height: 8.h),
          type == 2
              ? Container(
                  margin: EdgeInsets.only(bottom: 10.w),
                  child: Text("申请时间：2022-03-25 13:59：31", style: setTextStyle(10, FontWeight.w300, Tools.fromHex("#333333"))),
                )
              : const SizedBox(),
          for (var goods in item['goodsInfo'])
            OrderGoodsItem(
              goodsImg: goods['img'] ?? '',
              title: goods['goodsName'] ?? '',
              tags: ["保税"],
              price: goods['price'] ?? 0,
              unit: goods['unit'] ?? '件',
              num: goods['buyNum'] ?? 0,
            ),
          type == 1
              ? Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      decoration: border_radis(Colors.white, borderColor: Tools.fromHex("#F97C36")),
                      padding: EdgeInsets.fromLTRB(4.w, 2.w, 4.w, 2.w),
                      child: Row(
                        children: [BaseImage(url: "save_icon.png", width: 14, height: 14), SizedBox(width: 2.w), Text("支持90天价保", style: setTextStyle(12, FontWeight.normal, Tools.fromHex("#F97C36")))],
                      ),
                    ),
                    solidButton("申请", textColor: Tools.fromHex("#F83F2D"), backgroundColor: Colors.white, borderColor: Tools.fromHex("#F83F2D"), width: 72, height: 28, () {
                      controller.apply(item['goodsInfo'][0]['goodsId'], item['id'], item['sn']);
                      baojiaSuccessPopup(context);
                    })
                  ],
                )
              : Container(
                  padding: EdgeInsets.all(5.w),
                  decoration: border_radis(Tools.fromHex("#F3F5F7")),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        children: [Icon(Icons.check_circle_rounded, color: Tools.fromHex("#20B759")), SizedBox(width: 2.w), Text("商品已经成功保价1次，价保总金额¥99", style: setTextStyle(12, FontWeight.w300, Tools.fromHex("#666666")))],
                      ),
                      // BaseInteractive(
                      //   onClick: () => {

                      //   },
                      //   childWidget: Text("查看保价详情",style: setTextStyle(10, FontWeight.w300, Tools.fromHex("#0256FF"))),
                      // )
                    ],
                  ),
                )
        ],
      ),
    );
  }
}
